package com.iot.ic.service;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import com.iot.ic.common.CustomException;
import com.iot.ic.common.Options;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.MatchPauseMapper;
import com.iot.ic.dao.MatchScenesInfoMapper;
import com.iot.ic.dao.RefereeInfoMapper;
import com.iot.ic.dao.TeamInfoMapper;
import com.iot.ic.model.IllegalInfo;
import com.iot.ic.model.Location;
import com.iot.ic.model.MatchPause;
import com.iot.ic.model.MatchScenesInfo;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.ScenesResult;
import com.iot.ic.model.StadiumResult;
import com.iot.ic.model.StadiumScenes;
import com.iot.ic.model.TeamInfo;

/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class MatchPauseServiceImpl implements MatchPauseService {

	
	@Autowired
	private MatchPauseMapper matchPauseMapper;
	
	
	@Autowired
	private StadiumResultService stadiumResultService;
	
	@Autowired
	private TeamService teamService;
	
	@Autowired
	SimpMessagingTemplate simpMessagingTemplate;
	

	
	@Override
	public int startMatchPause(MatchPause matchPause) {
		int result = matchPauseMapper.insert(matchPause);
		if (result > 0) {
			result = matchPause.getMatchPauseId();
		}
		
		//修改比赛状态为暂停状态
		StadiumResult stadiumResult = new  StadiumResult();
		stadiumResult.setStadiumResultId(matchPause.getStadiumResultId());
		stadiumResult.setMatchState(2);
		stadiumResultService.updateStadiumResult(stadiumResult);
		
		//修改车队状态
		TeamInfo team = new TeamInfo();
		team.setTeamId(matchPause.getTeamId());
		team.setTeamState(Options.Pause.getCode());
		teamService.updateTeam(team);
		
		//websocket车队状态修改
		Map<String,Integer> teamStateMap = new HashMap<String,Integer>();
		teamStateMap.put("teamState", Options.Pause.getCode());
		simpMessagingTemplate.convertAndSendToUser(matchPause.getTeamId().toString(), "/testTeamStatesocketpush", teamStateMap);
		
		return result;
	}

	@Override
	public int endMatchPause(MatchPause matchPause) {
		MatchPause oldMatchPause = matchPauseMapper.selectByPrimaryKey(matchPause.getMatchPauseId());
		
		int result = matchPauseMapper.updateByPrimaryKeySelective(matchPause);
		
		//修改比赛状态为暂停状态
		StadiumResult stadiumResult = new  StadiumResult();
		stadiumResult.setStadiumResultId(oldMatchPause.getStadiumResultId());
		stadiumResult.setMatchState(0);
		stadiumResultService.updateStadiumResult(stadiumResult);
		
		
		//修改车队状态
		TeamInfo team = new TeamInfo();
		team.setTeamId(oldMatchPause.getTeamId());
		team.setTeamState(Options.Normal.getCode());
		teamService.updateTeam(team);
		
		//websocket车队状态修改
		Map<String,Integer> teamStateMap = new HashMap<String,Integer>();
		teamStateMap.put("teamState", Options.Normal.getCode());
		simpMessagingTemplate.convertAndSendToUser(oldMatchPause.getTeamId().toString(), "/testTeamStatesocketpush", teamStateMap);
		
		return result;
	}

	@Override
	public List<MatchPause> listMatchPause(MatchPause matchPause) {
		List<MatchPause> matchPauseList = matchPauseMapper.listMatchMatchPause(matchPause);
		return matchPauseList;
	}





	


	
	
	
	
	
}
